/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vista.TareaRealizada;

import Modelo.Entidades.Tarea;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

/*
 *  Proyecto eJAM [G12]
 *  Miembros:
 *      - Javier Ruiz Hidalgo
 *      - Adrian Ramos Cápitas
 *      - Miguel Alejandro Esteban Ordóñez
 */

public class TareaTableModel implements TableModel {


    private List<Tarea> tareas = new ArrayList<Tarea>();
    private List<TableModelListener> tableListeners = new ArrayList<TableModelListener>();

    //Implementacion Singleton
    private static TareaTableModel model=null;

    private TareaTableModel(){}

    public static TareaTableModel create(){
        if(model==null)
            model=new TareaTableModel();
        return model;
    }

    public List<Tarea> getTareas() {
        return tareas;
    }

    public void setTareas(List<Tarea> tareas) {
        this.tareas = tareas;
        fireContentsChanged();
    }

    //--------------- MÉTODOS PROPIOS DE TableModel -------------------------

    public Tarea getRow(int indexRow) {
        return tareas.get(indexRow);
    }

    public int getRowCount() {
        return tareas.size();
    }

    public int getColumnCount() {
        return 6; //ya que tendremos seis valores a mostrar por contacto
    }

    public String getColumnName(int columnIndex) {
        String name = null;
        
        switch (columnIndex) {
           case 0:
                name = "Nombre";
                break;
            case 1:
                name = "Tipo";
                break;
            case 2:
                name = "Fecha Finalización";
                break;
            case 3:
                name = "Duración";
                break;
            case 4:
                name = "Coeficiente Error";
                break;
            case 5:
                name = "Nº de Preguntas";
                break;
        }
        return name;
    }

    public Class<?> getColumnClass(int columnIndex) {
        Class<?> clase = null;

        switch (columnIndex) {
            case 0:
                clase = String.class;
                break;
            case 1:
                clase = String.class;
                break;
            case 2:
                clase = String.class;
                break;
            case 3:
                clase = String.class;
                break;
            case 4:
                clase = Double.class;
                break;
            case 5:
                clase = Integer.class;
                break;
        }
        return clase;
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false; //No permito editar celdas
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Tarea tarea = tareas.get(rowIndex);
        Object value = null;

        switch (columnIndex) {
            case 0:
                value = tarea.getNombre();
                break;
            case 1:
                value = tarea.getTipo();
                break;
            case 2:
                SimpleDateFormat formatoFecha = new SimpleDateFormat("dd/MM/yyyy HH:mm");
                value = formatoFecha.format(tarea.getF_fin());
                break;
            case 3:
                SimpleDateFormat formatoHora = new SimpleDateFormat("HH:mm");
                value = formatoHora.format(tarea.getDuracion());
                break;
            case 4:
                value = tarea.getC_error();
                break;
            case 5:
                value = tarea.getPreguntas().size();
                break;
        }
        return value;
    }

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void addTableModelListener(TableModelListener l) {
        tableListeners.add(l);
    }

    public void removeTableModelListener(TableModelListener l) {
        tableListeners.remove(l);
    }

    protected void fireContentsChanged() {
        //TableModelEvent event = new TableModelEvent(this,0,getRowCount());
        TableModelEvent event = new TableModelEvent(this, 0, this.getRowCount() - 1, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT);
        for (TableModelListener listener : tableListeners) {
            listener.tableChanged(event);
        }
    }
}
